package com.ruoyi.web.controller.sso;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LogoutSsoController {
    @Autowired
    private TokenService tokenService;

    @GetMapping("/logoutSso")
    public AjaxResult logoutSso(String token) {
        LoginUser loginUser = tokenService.getLoginUserBySsoToken(token);
        if (StringUtils.isNotNull(loginUser)) {
            String userName = loginUser.getUsername();
            // 删除用户缓存记录
            tokenService.delLoginUser(loginUser.getToken());
            // 记录用户退出日志
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGOUT, "单点注销退出成功"));
            System.out.println("单点注销退出成功：" + loginUser.getToken());
        }
        return AjaxResult.success("单点注销退出成功");
    }

}
